package cn.share.life.module.images.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class DBMain {

    public static void main(String[] args) throws SQLException, IOException {
        String allFilePath = "G:/青豫直流档案数据07241512/2.青豫直流";

        List<File> fileList = getImageFileNameMap(allFilePath);
        System.out.println("文件数量：" + fileList.size());

        String basePath = "G:/青豫直流/";
        List<Entity> fileItem = Db.use().findAll("file_item");
        System.out.println(fileItem);

        List<Entity> docFileList = Db.use().findAll("project_doc_file");
        List<Entity> docFileList2 = Db.use().findAll("project_document_engineering");

        Map<String, String> docMap = docFileList.stream()
                .filter(t -> StringUtils.isNotEmpty(t.getStr("minio_file_id")))
                .collect(Collectors.toMap(t -> t.getStr("minio_file_id"),
                t -> t.getStr("offline_path"), (a, b) -> a));
        Map<String, String> docMap2 = docFileList2.stream()
                .filter(t -> StringUtils.isNotEmpty(t.getStr("minio_file_id")))
                .collect(Collectors.toMap(t -> t.getStr("minio_file_id"),
                t -> t.getStr("offline_path"), (a, b) -> a));

        int count = 0;
        for (Entity entity: fileItem) {
//            System.out.print(entity.get("id"));
//            System.out.print("  --  ");
//            System.out.print(entity.get("url"));
//            System.out.print("  --  ");
//            System.out.print(entity.get("file_name"));
//            System.out.print("  --  ");
//            System.out.println(entity.get("file_size"));

            String filePath = basePath + entity.get("url");
            File file = new File(filePath);
            if (file.exists()) {
//                System.out.println("文件已存在：" + filePath);
                continue;
            }
            FileUtil.mkdir(file.getParentFile().getPath());

            String id = entity.getStr("id");
            String docPath = docMap.get(id);
            if (StringUtils.isEmpty(docPath)) {
                docPath = docMap2.get(id);
            }

            docPath = docPath.replace("~", "～");
            File itemFile = new File("G:/" + docPath);
            if (!itemFile.exists()) {

                docPath = docPath.replace("～", "~");
                itemFile = new File("G:/青豫直流档案数据07241512/" + docPath);
                if (!itemFile.exists()) {
                    count++;
                    System.out.println("未找到文件：" + docPath);
                    continue;
                }
            }

            // 更新文件大小
            long length = itemFile.length();
            System.out.println("文件大小：" + length + " 字节");

            Db.use().update(Entity.create().set("file_size", length),
                    Entity.create("file_item").set("id", entity.get("id")));
            Files.copy(itemFile, new File(filePath));
        }
        System.out.println("未找到文件数量：" + count);
    }

    private static File getFileByName(String fileName, List<File> fileList) {

        for (File file: fileList) {
            String name = file.getName();
            if (name.contains(fileName) || fileName.contains(name)) {
                return file;
            }
        }
        return null;
    }

    public static List<File> getImageFileNameMap(String path) {
        List<File> fileList = Lists.newArrayList();
        File directory = new File(path);

        if (directory.exists() && directory.isDirectory()) {
            for (File file : Objects.requireNonNull(directory.listFiles())) {
                if (file.isDirectory()) {
                    fileList.addAll(getImageFileNameMap(file.getAbsolutePath()));
                } else {
                    fileList.add(file);
                }
            }
        }
        return fileList;
    }
}
